<?php
class My_EventController extends MM_Controller_My_Action
{	
	/**
	 * @var MM_Service_Admin_Event
	 */
	private $serviceE = null;
		
	/**
	 * 初始化
	 */
	public function init()
	{
		parent::init();
		$this->serviceE = new MM_Service_Admin_Event();
	}
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$this->helper->setPageView($this->pageview, WEB_TITLE, "eventList", null);
	}
	
	/**
	 * page
	 */
	public function pageAction()
	{
		if ( !empty($this->httpdata['act']) && isset($this->httpdata['act']) ) {
			$dt = new Mediad2_Utility_Datatable($this->serviceE);
			if(!isset($_GET['ovar'])) {
				$_GET['ovar'] = "startdate";
			}
			$where = "uid = $this->uid";
			$dt->init($this->httpdata['act'], array("name"),$where);
			$dt->create();
			$dt->response();
		}
	}
	
	/**
	 * list event
	 */
	public function listAction()
	{
		if ( ($this->uid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid');
		} else {
			$where = "uid = $this->uid";
			$order = "eid asc";
			$this->result = $this->serviceE->fetchAll(null,null,$where,$order,true);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * create event
	 */
	public function createeventAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['name']) || empty($this->httpdata['name'])
				|| !isset($this->httpdata['startdate']) || empty($this->httpdata['startdate'])
				|| !isset($this->httpdata['enddate']) || empty($this->httpdata['enddate'])
				|| !isset($this->httpdata['address']) || empty($this->httpdata['address']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid...');
		} else {
			//保存event
			$this->result = $this->serviceE->createEvent($this->httpdata);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * update event
	 */
	public function updateeventAction()
	{
		if ( ($this->uid<1) || ($this->eid<1)
				|| !isset($this->httpdata['name']) || empty($this->httpdata['name'])
				|| !isset($this->httpdata['startdate']) || empty($this->httpdata['startdate'])
				|| !isset($this->httpdata['enddate']) || empty($this->httpdata['enddate'])
				|| !isset($this->httpdata['address']) || empty($this->httpdata['address']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid...');
		} else {
			$this->result = $this->serviceE->updateEvent($this->eid, $this->httpdata);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete event
	 */
	public function deleventAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$this->result = $this->serviceE->delEvent($this->eid, $this->uid);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * create event contact
	 */
	public function addcontactAction()
	{
		$name = @$this->httpdata['name'];
		$phone1 = @$this->httpdata['tel1'];
		$phone2 = @$this->httpdata['tel2'];
		if ( $this->uid<1	|| $this->eid<1 || !isset($name) || empty($name)) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid/name');
		} else {
			//保存event contact
			$service = new MM_Service_Admin_EventContact();
			$model = new MM_Model_EventContact();
			$model->setName($name);
			$model->setEid($this->eid);
			if(!$phone1)$phone1="";
			$model->setPhone($phone1);
			if($phone2)$model->setExtphone($phone2);
			$model->setNum(1);
			$this->result = $service->insert($model);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete event
	 */
	public function delcontactAction()
	{
		$ectid = $this->httpdata['ectid'];
		if ( $this->uid<1 || $this->eid<1 || !isset($ectid)) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid/ectid');
		} else {
			$service = new MM_Service_Admin_EventContact();
			$where = sprintf("ectid = '%s' and eid = '%s'",$ectid,$this->eid);
			$this->result = $service->deleteBy($where);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event follower关注者
	 */
	public function geteventfollowerAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$addtime = empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
			$service = new MM_Service_Admin_EventFollower();
			$this->result = $service->getEventFollower($this->eid, $addtime);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event user来宾
	 */
	public function geteventuserAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$addtime = empty($this->httpdata['addtime'])?0:$this->httpdata['addtime'];
			$service = new MM_Service_Admin_EventUser();
			$this->result = $service->getEventUser($this->eid, $addtime);
		}
		echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event info
	 */
	public function geteventinfoAction()
	{
		if ( ($this->uid<1) || ($this->eid<1) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$this->result = $this->serviceE->getEventInfo($this->eid);//cparty,event_list
		}
		echo $this->encodeResult($this->result);
	}

	/**
	 * get event ids[获取指定用户所有活动id数组]
	 */
	public function geteventbyuserAction()
	{
		if ( $this->uid<1) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$this->result = $this->serviceE->getEventIds($this->uid);
		}
		echo $this->encodeResult($this->result);
	}
	
	public function getappAction(){
		$code = @$this->httpdata['code'];
		if ( ($this->uid<1 || $this->eid<1 || !$code )) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
		} else {
			$this->result = $this->serviceE->getapp($code);
		}
		echo $this->encodeResult($this->result);
	}
	
	public function resetphotoAction(){//更新数据库旧格式的图片 20130808
		$p1 = @$this->httpdata['p1'];
		$p2 = @$this->httpdata['p2'];
		if ( ($this->uid<1  || $this->eid<1 )) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/code');
		} else {
			
			$file = new MM_Service_Admin_EventFiles();
			$service = new Mediad2_Utility_FileHandle();
			
			$where = sprintf("eid = '%s'",$this->eid);
			if($p1&&p2){				
				$r1 = $file->fetchLimit($p1,$p2,$where,null,true);
			}else{
				$r1 = $file->fetchAll(null,null,$where,null,true);
			}
			$arr = $r1->getData();
			$count = count($arr);
			$photo = null;
			if($count>0){
				foreach ($arr as $key => $value) {
					$path 	= $arr[$key]['filepath'];
					$ff 	= FILE_LOCATION.$path;
					$thumb 	= FILE_LOCATION."thumb/".$path;
					$review = FILE_LOCATION."review/".$path;
					$photo[$key]['file'] = $ff;
					$this->handlefileAction("del", $thumb);
					$this->handlefileAction("del", $review);
					$service->makeSamllPicture($ff, $thumb,$review,Array(220,220),Array(640,640));
				}
				$this->result = $this->returnResult(1,"make ".$count." photos".time(),$photo);
			}else{				
				$this->result = $this->returnResult(0,"have not any photo");
			}
		}
		echo $this->encodeResult($this->result);
	}
	
	private function handlefileAction ($fn,$source,$target=null){
		switch($fn){
			case "del":
				if (file_exists($source)){
					unlink($source);
				} 			
				break;
		}
	}
}